#pragma once
#include <iostream>
#include <cstdlib>
#include <vector>
#include "Task.hpp"
#include <unistd.h>
#include <sys/wait.h>
class Channel
{
public:
    Channel(int fd, pid_t id) : _wfd(fd), _subpid(id)
    {
        _name = "channel-" + std::to_string(_wfd) + "-" + std::to_string(_subpid);
    }
    ~Channel()
    {
    }
    void Send(int code)
    {
        int n = write(_wfd, &code, sizeof(code));
        (void)n;
    }
    void Close() {
        close(_wfd);
    }
    void WaitPid()
    {
        pid_t rid = waitpid(_subpid,nullptr,0);
        (void) rid;
    }
    int Fd() { return _wfd; }
    pid_t Subpid() { return _subpid; }
    std::string Name() { return _name; }

private:
    int _wfd;
    pid_t _subpid;
    std::string _name;
};

class ChannelManger
{
public:
    ChannelManger()
        : _next(0)
    {
    }
    void Insert(int wfd, pid_t subpid)
    {
        _channel.emplace_back(wfd, subpid);
    }
    Channel &Select()
    {
        auto &c = _channel[_next];
        _next++;
        _next %= _channel.size();
        return c;
    }
    void PrintChannel()
    {
        for (auto &channel : _channel)
        {
            std::cout << channel.Name() << std::endl;
        }
    }
    void StopChannel()
    {
        for(auto& channel: _channel) {
            channel.Close();
        }
    }
    void WaitSubPid()
    {
        for(auto& channel:_channel) {
            channel.WaitPid();
        }   
    }
    ~ChannelManger() {}
private:
    std::vector<Channel> _channel;
    int _next;
};

const int Channel_num_default = 5;
class ProcessPool
{
public:
    ProcessPool(int num)
        : _process_num(num)
    {
        _tm.Register(Print1);
        _tm.Register(Print2);
        _tm.Register(Print3);
    }
    void Work(int rfd)
    {
        while (true)
        {
            int code = 0;
            ssize_t n = read(rfd, &code, sizeof(code));
            if (n > 0)
            {
                if (n != sizeof(code))
                {
                    continue;
                }
            
                std::cout << "子进程[" << getpid() <<"]收到一个任务码" << code << std::endl;
                _tm.Execute(code);
                
            }
            else if (n == 0) {
                std::cout << "子进程退出" << std::endl;
                break;
            }
            else{
                std::cout << "读取错误" << std::endl;
                break;
            }
        }
    }
    bool Start()
    {
        for (int i = 0; i <= _process_num; i++)
        {
            int pipefd[2] = {0};
            int n = pipe(pipefd);
            if (n < 0)
            {
                std::cout << "创建失败" << std::endl;
                return false;
            }

            pid_t fd = fork();
            if (fd == 0)
            {
                // child
                close(pipefd[1]);
                Work(pipefd[0]);
                close(pipefd[0]);
                exit(0);
            }else if (fd < 0) {
                return false;
            }
            else
            {
                close(pipefd[0]);
                _cm.Insert(pipefd[1], fd);
            }
           
        }
        return true;
    }
    void Run()
    {
        // 1.选择一个任务
        int taskcode = _tm.Code();
        // 2.选择一个信道
        auto &c = _cm.Select();
        std::cout << "选择了一个信道:" << c.Fd() << std::endl;
        // 3.发送任务
        c.Send(taskcode);
        std::cout << "选择了一个任务码:" << taskcode << std::endl;
    }
    void Debug()
    {
        _cm.PrintChannel();
    }
    void Stop() {
        _cm.StopChannel();
        _cm.WaitSubPid();
    }
    ~ProcessPool()
    {
    }

private:
    ChannelManger _cm;
    int _process_num;
    TaskManger _tm;
};